

#import "SettingViewController.h"
#import "SettingViewCell.h"
#import "SugestionViewController.h"
#import "MBProgressHUD+HM.h"

@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *listArr;

@end

static NSString *SettingCell = @"SettingCell";

@implementation SettingViewController

- (NSArray *)listArr{
    if (!_listArr) {
        _listArr = @[@"意见反馈",@"加号符号说明",@"修改密码",@"注销用户"];
    }
    return _listArr;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200+64)];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        [_tableView registerNib:[UINib nibWithNibName:@"SettingViewCell" bundle:nil] forCellReuseIdentifier:SettingCell];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    
}


#pragma mark - dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SettingCell forIndexPath:indexPath];
    
    cell.lb_desc.text = self.listArr[indexPath.row];
    
    return cell;
}


#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        SugestionViewController *sugestionVC = [[SugestionViewController alloc] init];
        
        [self.navigationController pushViewController:sugestionVC animated:YES];
    }
    
    //注销
    if (indexPath.row == 3) {
        
        //判断当前是否是登录状态
        //读取登录状态
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        self.loginSuc = [defaults boolForKey:@"loginSuc"];
        
        if (self.loginSuc) {
            [self showAlertWithTitle:@"确定注销吗?" message:@""];
            
        }else{
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD showErrorMessage:@"当前还未登录"];
            });
        }
    }
}



//方法子
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionY = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *actionN = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //注销回到主页面
        BaseNaviController *nvc = (BaseNaviController *)self.sidePanelController.centerPanel;
        [nvc popViewControllerAnimated:YES];
        
        //发出通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kNotification" object:nil userInfo:@{@"isCancel":@YES}];
        
        //发通知改注册状态的值
        [[NSNotificationCenter defaultCenter]postNotificationName:@"issave" object:0];
        
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:NO forKey:@"autoLogin"];
        
    }];
    [alert addAction:actionY];
    [alert addAction:actionN];
    [self presentViewController:alert animated:YES completion:nil];
}



@end
